package tt.dz.scheduletask.tasks;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import tt.dz.dao.base.BaseDao;
import tt.dz.dao.ext.charger.OmsChargerStateAlternateDao;
import tt.dz.dao.ext.charger.OmsChargerStateDirectDao;
import tt.dz.entity.charger.OmsChargerStateAlternate;
import tt.dz.entity.charger.OmsChargerStateDirect;
import tt.dz.scheduletask.ScheduledTask;
import tt.dz.util.RedisClient;

import javax.annotation.PostConstruct;

//定时踢下-设置在线状态为0，如果2分钟内没有没有心跳或没有充电上传记录的桩
@Component
public class KickOffLineChargerTask implements ScheduledTask
{
	private Log log = LogFactory.getLog(KickOffLineChargerTask.class);
	//心跳数据存在redis中，一般redis不重启。桩和最新心跳时间对应的map。目前不会超过500,
	//map里面第一个参数是chargerId,第二个是时间 date.getTime()
	public static HashMap<String,String> chargerHeartMap ;

	//库里的桩和在线状态对应的map。目前不会超过1000
//	public static HashMap<String,Integer> chargerDbMap = new HashMap<String,Integer>(1000);
	
	@Autowired
	OmsChargerStateAlternateDao alterDao;
	@Autowired
	OmsChargerStateDirectDao directDao;
	@Autowired
	BaseDao baseDao;
//	当前时间12点30  心跳应该是12点28 12点29

	@PostConstruct
	public void init(){//查询所有的医院
		chargerHeartMap = new HashMap(500);
	}

	@Override
	public void execute()
	{
		if(RedisClient.getMap("heart")==null){
			RedisClient.putMap("heart",new HashMap<String,String>(500));
		}
		log.info("KickOffLineChargerTask 开始执行 chargerHeartMap.size() ="+chargerHeartMap.size());
		Date now = new Date();
//		Date afterDate = new Date(now.getTime() - 120000);//当前时间减去2分钟
		long nowLong = now.getTime();
		StringBuilder alterOffline = new StringBuilder("");
		StringBuilder directOffline = new StringBuilder("");
		try
		{
			//交流:只查询在线的
//			List<OmsChargerStateAlternate> alterList = alterDao.findAll(new OmsChargerStateAlternate());
			List<OmsChargerStateAlternate> alterList = alterDao.findListByCondition(new OmsChargerStateAlternate(),"where online_status = 1 ");
			log.info("alterList.size() = "+alterList.size());
			for(OmsChargerStateAlternate alternate :alterList){
//				chargerDbMap.put(alternate.getCharger_id(), alternate.getOnline_status());
//				Integer onlineStatus = alternate.getOnline_status();
//				if(onlineStatus!=null && onlineStatus.intValue() == 1){
					String dateStr = chargerHeartMap.get(alternate.getCharger_id());
					//如果没心跳，或者心跳时间比当前时间早了2分钟，就认为掉线
					if((dateStr==null) || (nowLong - Long.valueOf(dateStr).longValue() > 120000) ){
						alterOffline.append(alternate.getCharger_id()+",");
					}
//				}
			}
			log.info("alterOffline = "+alterOffline);
			if(alterOffline.length()>1){
				alterOffline.deleteCharAt(alterOffline.length()-1);
				alterDao.exec("update oms_charger_state_alternate set online_status = 0 where charger_id in ("+alterOffline.toString()+")");
			}
			//直流
//			List<OmsChargerStateDirect> directList = directDao.findAll(new OmsChargerStateDirect());
			List<OmsChargerStateDirect> directList = directDao.findListByCondition(new OmsChargerStateDirect(),"where online_status = 1 ");
			log.info("directList.size() = "+directList.size());
			for(OmsChargerStateDirect direct :directList){
//				chargerDbMap.put(direct.getCharger_id(), direct.getOnline_status());
//				Integer onlineStatus = direct.getOnline_status();
//				if(onlineStatus!=null && onlineStatus.intValue() == 1){
					String dateStr = chargerHeartMap.get(direct.getCharger_id());
					//如果没心跳，或者心跳时间比当前时间晚，或者心跳时间比当前时间早了2分钟，就认为掉线
					if((dateStr==null) || (nowLong - Long.valueOf(dateStr).longValue() > 120000) ){
						directOffline.append(direct.getCharger_id()+",");
					}
//				}
			}
			log.info("directOffline = "+directOffline);
			if(directOffline.length()>1){
				directOffline.deleteCharAt(directOffline.length()-1);
				directDao.exec("update oms_charger_state_direct set online_status = 0 where charger_id in ("+directOffline.toString()+")");
			}
		}
		catch (Exception e)
		{
			log.info("KickOffLineChargerTask 执行失败");
			log.error(e.getMessage(), e);
		}
	}
}
